package view;

import java.awt.GridLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.FileParser;
import model.Manuscript;
import model.Review;
import model.State;

@SuppressWarnings("serial")
/*
 * Panel specific to user type Reviewer. Shows papers 
 * assigned to reviewer and reviews completed.
 * @author Jeff Kuhlman, Kevin Anderson
 */
public class ReviewerPanel extends JPanel implements Observer{
	
	
	
	private static final String[] headTitles = new String[] 
			{ "Paper Status","Titler","Open Paper","Upload Review" };
	private static final int cols = headTitles.length;

	/*
	 * Current state of user, including user id, conference,
	 * and role.
	 */
	private State my_state;
	
	/*
	 * File parser object.
	 */
	private FileParser my_fp;
	
	/*
	 * Papers associated with a conference.
	 */
	private Manuscript[] my_papers;
	
	/*
	 * Name of each associated paper.
	 */
	private String my_paper_name;
		
	public ReviewerPanel(State the_state) {
		super();
		my_state = the_state;
		my_fp = my_state.getParser();
		my_fp.addObserver(this);
		setup();
	}
	
	public void setup() {
		GridLayout exp_layout = new GridLayout(0,4);
		this.setLayout(exp_layout);
		int user_id = my_state.getUser().getID();
		int conf_id = my_state.getConference().getConfID();
		my_papers = my_fp.getPapersByReviewer(user_id, conf_id);
		if (my_papers.length == 0) {
			JOptionPane.showMessageDialog(this, "You currently have no papers assigned to you. Please contact your SPC");
		} else {
			JLabel[] headers = new JLabel[cols];
			for(int i = 0; i < cols; i++)
			{
				headers[i] = new JLabel(headTitles[i]);
				headers[i].setHorizontalAlignment(JLabel.CENTER);
				add(headers[i]);
			}
		}

		for (int i = 0; i < my_papers.length; i++) {
			my_paper_name = my_papers[i].getMy_title();
			JLabel pn = new JLabel(my_paper_name);
			OpenPaperAction opa = new OpenPaperAction(my_papers[i]);
			JButton open = new JButton(opa);
			open.setText("Open Paper");
			
			//MAKE DECISION HERE TO EITHER SHOW THE OPTION TO ADD A REVIEW OR TO ALLOW OPEN OF REVIEW			
			Review[] review = my_fp.getReviewsByPaperID(my_papers[i].getMy_id());
			
			int openIndex = -1;
			for (int j = 0; j < review.length; j++) //search for a review that was done by me
			{		
				if (review[j].getMy_reviewerID() == my_state.getUser().getID())
					openIndex = j;
				
			}
			
			
			JButton toggleButton = new JButton();
			
			if (openIndex >= 0) { //A REVIEW WAS FOUND
				toggleButton.addActionListener(new OpenPaperAction(review[openIndex]));
				toggleButton.setText("Open Review");
			} else { //NO REVIEW WAS FOUND
				toggleButton.addActionListener(new NewReviewAction(my_state, my_papers[i]));
				toggleButton.setText("+Review");
			}
			
			
			this.add(new JLabel(my_papers[i].getMy_status().toString()));
			this.add(pn);
			this.add(open);
			this.add(toggleButton);
		}
	}

	@Override
	public void update(Observable o, Object arg) {
		removeAll();
		setup();
		validate();
		repaint();
		
	}
	

}